<?php
/**
 * Model for the routes 
 * 
 * @author Simon, Denis, Dennis
 *
 */
class Routes_model extends CI_Model {  
  	/**
  	 * 
  	 *Constructor 
  	 */
    function Routes_model()  
    {  
        $this->load->database();
    }  
  
      /**
       * 
       * returns a Route by the routeID
       * @param  $routeId ID of the route
       */  
      function getRouteByID($routeId)  
        {  
            //Query the data table for every record and row  
            
  			$query = $this->db->get_where('routes', array('ID' => $routeId));
  			
            if ($query->num_rows() < 1)  
            {  
                //show_error('Database is empty!');  
            }else{  
                return $query->row();  
            }  
        }  
        /**
         * 
         * Returns all routes of a user
         * @param $userId ID of the user
         */
        function getRouteFromUser($userId)  
        {  
            //Query the data table for every record and row  
            
  			$query = $this->db->get_where('routes', array('OwnerID' => $userId));
  			
            if ($query->num_rows() < 1)  
            {  
                //show_error('Database is empty!');  
            }else{  
                return $query->result();  
            }  
        }  
        /**
         * 
         * Adds a route to the db
         * @param  $ownerID Owner ID
         * @param  $name Name
         * @param  $routeFile File where the points are stored in
         * @param  $difficulty Difficulty
         * @param  $ground Ground
         * @param  $length Length in m
         * @param  $public Boolean is Public?
         */
        function addRoute($ownerID,$name,$routeFile,$difficulty,$ground,$length,$public,$favorite)  
        {  
            $data = array(
			   'OwnerID' =>$ownerID,
			   'Name' => $name,
			   'RouteFile' => $routeFile,
               'Difficulty' => $difficulty,
               'Ground' => $ground,
               'Length' => $length,
               'Public' => $public,
               'Favorite' => $favorite
			);

			return $this->db->insert('routes', $data);
        	
        } 
        /**
         * 
         * deletes a route by ID
         * @param  $routeID Id of the route
         */
        function delRoute($routeID){
        	return $this->db->delete('routes', array('ID' => $routeID));
        	
        }
        
        /**
        *
        * Saves a route to the db
        * @param  $ownerID Owner ID
        * @param  $name Name
        * @param  $routeFile File where the points are stored in
        * @param  $difficulty Difficulty
        * @param  $ground Ground
        * @param  $length Length in m
        * @param  $public Boolean is Public?
        */
        function saveRoute($routeID,$difficulty,$ground,$public,$favorite)
        {
        	$data = array(
                       'Difficulty' => $difficulty,
                       'Ground' => $ground,
                       'Public' => $public,
        			   'Favorite' => $favorite
        	);
        
        	$this->db->where('ID', $routeID);
        	return $this->db->update('routes', $data);
        	 
        }
  
}  
?>  